close all;
addpath('D:\Codes\SceneInt\Project');
%% load image and annotation (need labelme toolbox)
filename = 'D:\Codes\Scene\Labelme\Annotations\scene_test\fantasy-vase.xml';
HOMEIMAGES = 'D:\Codes\Scene\Labelme\Images';
Objects = loadXML(filename);

%% make saliency map
[Img, errmsg]=initializeImage([HOMEIMAGES '\' Objects.annotation.folder '\' Objects.annotation.filename]);
temp=Img.data;
% figure; imshow(Img.data);
% Img.data=zeros(size(temp,1),size(temp,2),3);
% Img.data(:,:,1)=uint8(temp(:,:,1));
% Img.data(:,:,2)=uint8(temp(:,:,2));
% Img.data(:,:,3)=uint8(temp(:,:,3));
Img.dims=3;
Img.size=size(Img.data);
% figure; imshow(Img.data);
salParams = defaultSaliencyParams(Img.size);
[salMap,salData] = makeSaliencyMap(Img,salParams);
% figure; imagesc(salMap.data);

% compute gravity center for each objects
Centroids=zeros(size(Objects.annotation.object, 2), 2);
for i=1: 1: size(Objects.annotation.object, 2)
    Points=Objects.annotation.object(1,i).polygon.pt;
    n_pts=size(Objects.annotation.object(1,i).polygon.pt,2);
    temp_pts=zeros(n_pts,2);
    for j=1:1:n_pts
        temp_pts(j,:)=[str2num(Points(1,j).x), str2num(Points(1,j).y)];
    end
    Centroids(i,:)=mean(temp_pts);
end
    
% transfer gravity center position into the index in the reduced saliency
% map
SalCentroids=zeros(size(Objects.annotation.object, 2), 2);
SalCentroids(:,1)=Centroids(:,1)*(size(salMap.data,1)/Img.size(1));
    % reference transformation since origin is located at the upper left corner.
SalCentroids(:,2)=Img.size(2)-Centroids(:,2);
SalCentroids(:,2)=Centroids(:,2)*(size(salMap.data,2)/Img.size(2));
SalCentroids=ceil(SalCentroids);

% combine the gravity center and the saliency map into a new saliency map
SalMask=zeros(size(salMap.data));
for i=1:size(Objects.annotation.object, 2)
    x=uint8(SalCentroids(i,2));
    y=uint8(SalCentroids(i,1));
    SalMask(x,y)=1;
    SalCentroids(i,:)=[x,y];    
end

% neural model of visual search
%     1. update eye field is replaced by drawXML4SceneInt
%     2. set rank to zero if visited, and draw it grey.
f_scene_search(filename, HOMEIMAGES, SalCentroids, SalMask);